<?php
class ItsStockIn extends AppModel {
    var $name = 'ItsStockIn';

    var $useTable = 'its_stocks_in';

    /**
     * ประเภทการ stock in ที่จะเก็บ ใน Product Serial
     */
    const STOCK_IN_TYPE = 'S';

    var $belongsTo = array(
        'Unit' => array(
            'fields' => array('id', 'title')
        ),
        'Product' => array()
    );

    var $hasMany = array(
      'ProductSerial' => array(
          'foreignKey' => 'its_stock_in_id',
          'conditions' => array(
              'its_stock_in_type' => self::STOCK_IN_TYPE
          )
      )
    );

    var $validate = array(
        'cost' => array(
            'rule' => 'numeric',
            'message' => 'Please input product cost.',
            'on' => 'create'
        ),
        'location_id' => array(
            'rule' => 'notEmpty',
            'message' => 'Please input a location for product.',
            'on' => 'create'
        ),
    );

    function beforeSave() {
        // Insert
        if (isset($this->data['ItsStockIn']['stock_in_date'])) {
            $this->data['ItsStockIn']['stock_in_date'] = $this->dateFormatBeforeSave($this->data['ItsStockIn']['stock_in_date']);
        }
        return true;
    }
}
?>
